package com.fjp.heartguard.app.util

import android.text.TextUtils
import com.fjp.heartguard.app.data.bean.OlderInfo
import com.fjp.heartguard.app.data.bean.UserInfo
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.tencent.mmkv.MMKV
import me.hgj.jetpackmvvm.ext.util.toJson
import java.lang.reflect.Type


object CacheUtil {


    /**
     * 是否已经登录
     */
    fun isLogin(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("login", false)
    }

    fun getPassword(): String {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeString("password", "")
    }
    fun getVersion(): String {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeString("version", "")
    }

    fun setVersion(version: String): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("version", version)
    }
    fun setPassword(password: String): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("password", password)
    }

    fun getMobile(): String {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeString("mobile", "")
    }

    fun setMobile(password: String): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("mobile", password)
    }

    /**
     * 设置是否已经登录
     */
    fun setIsLogin(isLogin: Boolean) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode("login", isLogin)
    }

    fun setVoiceInteraction(isOpen: Boolean): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("voiceInteraction", isOpen)
    }

    fun getVoiceInteraction(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("voiceInteraction", true)
    }

    /**
     * 获取保存的账户信息
     */
    fun getUser(): UserInfo? {
        val kv = MMKV.mmkvWithID("app")
        val userStr = kv.decodeString("user")
        return if (TextUtils.isEmpty(userStr)) {
            null
        } else {
            Gson().fromJson(userStr, UserInfo::class.java)
        }
    }

    /**
     * 设置账户信息
     */
    fun setUser(userInfo: UserInfo?) {
        val kv = MMKV.mmkvWithID("app")
        if (userInfo == null) {
            kv.encode("user", "")
            setIsLogin(false)
        } else {
            kv.encode("user", Gson().toJson(userInfo))
            setIsLogin(true)
        }
    }

    fun addOlderInfo(olderInfo: OlderInfo) {
        val kv = MMKV.mmkvWithID("app")
        val oldOlderInfoJson = kv.decodeString("olderInfoList", "[]")
        val stringArrayType: Type = object : TypeToken<ArrayList<OlderInfo>>() {}.type
        val olderinfoList: ArrayList<OlderInfo> = Gson().fromJson(oldOlderInfoJson, stringArrayType)
        olderinfoList.add(olderInfo)
        kv.encode("olderInfoList", olderinfoList.toJson())

    }


    fun removeFirstOlderInfo() {
        val kv = MMKV.mmkvWithID("app")
        val oldOlderInfoJson = kv.decodeString("olderInfoList", "[]")
        val stringArrayType: Type = object : TypeToken<ArrayList<OlderInfo>>() {}.type
        val olderinfoList: ArrayList<OlderInfo> = Gson().fromJson(oldOlderInfoJson, stringArrayType)
        if (olderinfoList.size > 0) {
            olderinfoList.removeFirst()
        }
        kv.encode("olderInfoList", olderinfoList.toJson())

    }

    fun getOlderInfoList(): ArrayList<OlderInfo> {
        val kv = MMKV.mmkvWithID("app")
        val odlerInfoListJSON = kv.decodeString("olderInfoList", "[]")
        val stringArrayType: Type = object : TypeToken<ArrayList<OlderInfo>>() {}.type
        val olderinfoList: ArrayList<OlderInfo> = Gson().fromJson(odlerInfoListJSON, stringArrayType)
        return olderinfoList
    }

    fun clearInfoList() {
        val kv = MMKV.mmkvWithID("app")
        kv.encode("olderInfoList", "[]")

    }
}